﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace ETRoboConResultEditorExcel
{
    public partial class Sheet8
    {
        private const int POS_TeamIdCol = 1;
    //    private const int POS_TeamBibCol = 2;
        private const int POS_TeamNameCol = 2;
        private const int POS_ModelPoint = 3;
        private const int POS_TeamNumCol = 2;
        private const int POS_TeamNumRow = 1;
        private const int POS_TeamDataStartRow = 3;


        private void Sheet8_Startup(object sender, System.EventArgs e)
        {
        }

        private void Sheet8_Shutdown(object sender, System.EventArgs e)
        {
        }

        public int AddTeamModelPoint(string teamId, int bib, string teamName, double? modelPoint, int index)
        {
            ((Excel.Range)Globals.Sheet8.Cells[POS_TeamDataStartRow + index, POS_TeamIdCol]).Value2 = teamId;
     //       ((Excel.Range)Globals.Sheet8.Cells[POS_TeamDataStartRow + index, POS_TeamBibCol]).Value2 = bib.ToString();
            ((Excel.Range)Globals.Sheet8.Cells[POS_TeamDataStartRow + index, POS_TeamNameCol]).Value2 = teamName;
            ((Excel.Range)Globals.Sheet8.Cells[POS_TeamDataStartRow + index, POS_ModelPoint]).Value2 = modelPoint;
            index++;
            ((Excel.Range)Globals.Sheet8.Cells[POS_TeamNumRow, POS_TeamNumCol]).Value2 = index;
            return index;
        }

        public int GetTeamModelPoint(out string teamId, out string teamName, out double? modelPoint,ref int index)
        {
            if (((Excel.Range)Globals.Sheet8.Cells[POS_TeamDataStartRow + index, POS_TeamIdCol]).Value2 != null)
            {
                teamId = ((Excel.Range)Globals.Sheet8.Cells[POS_TeamDataStartRow + index, POS_TeamIdCol]).Value2.ToString();
                if (string.IsNullOrEmpty(teamId))
                {
//                    bib = -1;
                    teamName = null;
                    modelPoint = null;
                    return -1;
                }
                else
                {
                    if (((Excel.Range)Globals.Sheet8.Cells[POS_TeamDataStartRow + index, POS_ModelPoint]).Value2 == null)
                    {
  //                      bib = -1;
                        teamName = null;
                        modelPoint = null;
                        index++;
                        return index;
                    }
          //          string value = ((Excel.Range)Globals.Sheet8.Cells[POS_TeamDataStartRow + index, POS_TeamBibCol]).Value2.ToString();
    //                bib = int.Parse(value);
                    teamName = ((Excel.Range)Globals.Sheet8.Cells[POS_TeamDataStartRow + index, POS_TeamNameCol]).Value2.ToString();
                    string value = ((Excel.Range)Globals.Sheet8.Cells[POS_TeamDataStartRow + index, POS_ModelPoint]).Value2.ToString();
                    double tryValue;
                    if (!double.TryParse(value, out tryValue))
                    {
                        MessageBox.Show(String.Format("ID[{0}]のチームのモデル評価ポイントが数字ではありません。修正してください。", teamId));
      //                  bib = -1;
                        teamName = null;
                        modelPoint = null;
                        return -1;
                    }
                    else
                    {
                        modelPoint = double.Parse(value);
                    }
                    index++;
                    return index;
                }
            }
            teamId = "";
        //    bib = -1;
            teamName = null;
            modelPoint = null;
            return -1;
        }
        #region VSTO で生成されたコード

        /// <summary>
        /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディターで変更しないでください。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Sheet8_Startup);
            this.Shutdown += new System.EventHandler(Sheet8_Shutdown);
        }

        #endregion

    }
}
